package org.pskovgu.pskovbus;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class BusDataSource {
	private SQLiteDatabase database;
	private Context context;
	private BusDbOpenHelper dbHelper;
	private String[] allColumns = { 
			  BusDbOpenHelper.BUS_ID,
			  BusDbOpenHelper.NUMBER,
			  BusDbOpenHelper.NAME,
			  BusDbOpenHelper.START_STATION,
			  BusDbOpenHelper.STOP_STATION
	};
	

	  public BusDataSource(Context context) {
	    dbHelper = new BusDbOpenHelper(context);
	    this.context = context;
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public Bus createBus(String number, String name) {
	    ContentValues values = new ContentValues();
	    values.put(BusDbOpenHelper.NUMBER, number);
	    values.put(BusDbOpenHelper.NAME, name);
	    long insertId = database.insert(BusDbOpenHelper.TABLE_NAME, null, values);
	    Cursor cursor = database.query(BusDbOpenHelper.TABLE_NAME,
	        allColumns, BusDbOpenHelper.BUS_ID + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    Bus newComment = cursorToBus(cursor);
	    cursor.close();
	    return newComment;
	  }

	  public void deleteBus(Bus comment) {
	    long id = comment.getId();
	    System.out.println("Bus deleted with id: " + id);
	    database.delete(BusDbOpenHelper.TABLE_NAME, BusDbOpenHelper.BUS_ID
	        + " = " + id, null);
	  }

	  public List<Bus> getAllBuses() {
	    List<Bus> comments = new ArrayList<Bus>();

	    Cursor cursor = database.query(BusDbOpenHelper.TABLE_NAME,
	        allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      Bus comment = cursorToBus(cursor);
	      comments.add(comment);
	      cursor.moveToNext();
	    }
	    // Make sure to close the cursor
	    cursor.close();
	    return comments;
	  }
	  
	  public List<Bus> getBusesById(long l) {
		    List<Bus> comments = new ArrayList<Bus>();
		    String[] args = {Integer.toString((int) l)};

		    Cursor cursor = database.query(BusDbOpenHelper.TABLE_NAME,
		        allColumns, BusDbOpenHelper.BUS_ID+" = ?", args, null, null, null);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		      Bus comment = cursorToBus(cursor);
		      comments.add(comment);
		      cursor.moveToNext();
		    }
		    // Make sure to close the cursor
		    cursor.close();
		    return comments;
		  }
	  

	  private Bus cursorToBus(Cursor cursor) {
	    Bus bus = new Bus();
	    bus.setId(cursor.getLong(cursor.getColumnIndex(BusDbOpenHelper.BUS_ID)));	    
	    bus.setNumber(cursor.getString(cursor.getColumnIndex(BusDbOpenHelper.NUMBER)));
	    bus.setName(cursor.getString(cursor.getColumnIndex(BusDbOpenHelper.NAME)));
	    
	    StationDataSource stationDS = new StationDataSource(context);
	    stationDS.open();
	    Station start = stationDS.getStationsById(cursor.getLong(cursor.getColumnIndex(BusDbOpenHelper.START_STATION))).get(0);
	    Station stop = stationDS.getStationsById(cursor.getLong(cursor.getColumnIndex(BusDbOpenHelper.STOP_STATION))).get(0);
	    bus.setStart(start);
	    bus.setStop(stop);
	    stationDS.close();
	    
	    return bus;
	  }

}
